package lifecycle;

import org.springframework.context.Lifecycle;
import org.springframework.context.support.GenericApplicationContext;

/**
 * Spring应用上下文生命周期
 * @author jackcfan
 * @date 2021/10/2
 */
public class ApplicationLifeCycleTest {
    public static void main(String[] args) {
        GenericApplicationContext context = new GenericApplicationContext();

        context.registerBean("myLifeCycle", MyLifeCycle.class);

        // 刷新
        context.refresh();

        // 启动
        context.start();

        // 停止
        context.stop();

        // 关闭
        context.close();
    }

    // 容器的生命周期
    static class MyLifeCycle implements Lifecycle{
        boolean isRunning = false;

        @Override
        public void start() {
            System.out.println("MyLifeCycle start...");
            isRunning = true;
        }

        @Override
        public void stop() {
            System.out.println("MyLifeCycle stop...");
            isRunning = false;
        }

        @Override
        public boolean isRunning() {
            return isRunning;
        }
    }
}
